<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$status = UV::readMQ(strip_tags($route->GET('status')));

switch ($status){
	case "must_confirm": $type_str = " where `U`.`status` = 'must_confirm'"; break;
	case "must_approve": $type_str = " where `U`.`status` = 'must_approve'"; break;
	case "approved": $type_str = " where `U`.`status` = 'approved'"; break;
	case "trusted": $type_str = " where `U`.`status` = 'trusted'"; break;
	case "banned": $type_str = " where `U`.`status` = 'banned'"; break;
	
	default: $type_str = "";
}

$sex_ar = array("male"=>"мужской","female"=>"женский","undefined"=>"-");
$types_ar = array("administrator"=>"Администратор","coordinator1"=>"Координатор 1 типа","coordinator2"=>"Координатор 2 типа","user"=>"Обычный пользователь");
$status_ar = array("must_confirm"=>"Ожидается подтверждение","must_approve"=>"Не проверен","approved"=>"Проверен","trusted"=>"Доверенный","banned"=>"Забаненный");

$query = "select `U`.*,`R`.`login` as `r_login`,`B`.`ban_time`,count(distinct `N`.`news_id`) as `news_count`,count(distinct `NC`.`nc_id`) as `comments_count`,`cities`.`name` as `city_name`
	from `users` as `U`
	left join `users` as `R` on
	`R`.`user_id` = `U`.`referer`
	left join `ban_users` as `B` on
	`B`.`user_id` = `U`.`user_id`
	left join `news` as `N` on
	`N`.`user_id` = `U`.`user_id`
	left join `news_comments` as `NC` on
	`NC`.`user_id` = `U`.`user_id`
	left join `cities` on
	`cities`.`city_id` = `U`.`city_id` $type_str
	group by `U`.`user_id` order by `U`.`user_id`";
$all_users = $db->getTable($query);

require_once ADMIN.DS."head.php";
?>
<div class="full">
	<div class='submenu_container'>
		<a class="submenu_button" href="/admin/users/short">Коротко</a>
		<a class="submenu_button submenu_button_active" href="/admin/users/full">Полностью</a>
	</div>
	
	<h3>Зарегистрированные пользователи</h3>
	<div class='group_container'>
		<a href="/admin/users/full" class="group all_users <?php echo ($status != "must_confirm" && $status != "must_approve" && $status != "approved" && $status != "trusted" && $status != "banned") ? "active" : "";?>">Все</a>
		<a href="?status=must_confirm" class="group wimg <?php echo ($status == "must_confirm") ? "active" : "";?>">Не подтвержденные</a>
		<a href="?status=must_approve" class="group wimg <?php echo ($status == "must_approve") ? "active" : "";?>">Не проверенные</a>
		<a href="?status=approved" class="group wimg <?php echo ($status == "approved") ? "active" : "";?>">Проверенные</a>
		<a href="?status=trusted" class="group wimg <?php echo ($status == "trusted") ? "active" : "";?>">Доверенные</a>
		<a href="?status=banned" class="group wimg <?php echo ($status == "banned") ? "active" : "";?>">Забаненные</a>
	</div>
	<div class='text-container'>
		<table class="message_list" style="line-height: 16pt;font-size:10pt;font-family:Arial">
			<tr class='messages_header'>
				<td align='center'>ID</td><td width='225px'>Пользователь</td><td>Информация</td><td>Активность</td><td class='actions'>Изм. статус</td>
			</tr><?php
				foreach ($all_users as $each_user){
					echo "<tr>";
						echo "<td align='center'>{$each_user['user_id']}</td>";
						echo "<td>";
							echo "<a name='user_{$each_user['user_id']}'></a>";
							echo "<span class='bolder'>Логин:</span> <span class='".($each_user['last_time'] > time() - TIME_ONLINE ? "online" : "offline")."'>{$each_user['login']}</span><br />";
							echo "<span class='bolder'>Тип:</span> <span>{$types_ar[$each_user['type']]}</span><br />";
							echo "<span class='bolder'>Статус:</span> <span>{$status_ar[$each_user['status']]}</span><br />";
							if ($each_user['status'] == "banned"){
								echo "<span style='margin-left: 20px'>(до: ".date("d.m.Y H:i",$each_user['ban_time']).")</span><br />";
							}
							echo "<span class='bolder'>Последний визит:</span> <span>".($each_user['last_time'] ? date("d.m.Y H:i",$each_user['last_time']) : "-")."</span><br />";
							echo "<span class='bolder'>IP:</span> <span>".($each_user['last_ip'] ? $each_user['last_ip'] : "-")."</span><br />";
							echo "<span class='bolder'>По приглашению:</span> <span>".($each_user['r_login'] ? $each_user['r_login'] : "-")."</span>";
						echo "</td>";
						echo "<td>";
							echo "<span class='bolder'>ФИО:</span> ".$each_user['sname']." ".$each_user['name']." ".$each_user['tname']."<br />";
							echo "<span class='bolder'>Email:</span> ".($each_user['email'] ? $each_user['email'] : "-")."<br />";
							echo "<span class='bolder'>День рождения:</span> ".($each_user['birthdate'] ? date("d.m.Y",$each_user['birthdate']) : "-")."<br />";
							echo "<span class='bolder'>Город:</span> ".($each_user['city_name'] ? $each_user['city_name'] : "-")."<br />";
							echo "<span class='bolder'>Пол:</span> ".$sex_ar[$each_user['sex']]."<br />";
							echo "<span class='bolder'>О себе:</span> ".($each_user['about'] ? $each_user['about'] : "-");
						echo "</td>";
						echo "<td>";
							echo "<span class='bolder'>Статей:</span> <span>{$each_user['news_count']}</span><br />";
							echo "<span class='bolder'>Комментариев:</span> <span>{$each_user['comments_count']}</span>";
						echo "</td>";
						echo "<td class='actions'>";
							if ($each_user['type'] != "administrator"){
								if ($each_user['status'] != "banned"){
									echo "<a href='/admin/users/change?status=banned&user_id={$each_user['user_id']}' class='send'>Забаненый</a><br />";
								}
								if ($each_user['status'] != "must_approve"){
									echo "<a href='/admin/users/change?status=must_approve&user_id={$each_user['user_id']}' class='send'>Не проверенный</a><br />";
								}
								if ($each_user['status'] != "approved"){
									echo "<a href='/admin/users/change?status=approved&user_id={$each_user['user_id']}' class='send'>Проверенный</a><br />";
								}
								if ($each_user['status'] != "trusted"){
									echo "<a href='/admin/users/change?status=trusted&user_id={$each_user['user_id']}' class='send'>Доверенный</a>";
								}
							} else {
								echo "не доступно";
							}
						echo "</td>";
					echo "</tr>";
				}
			?>
		</table>
	</div>
</div>
<?php require_once ADMIN.DS."footer.php"; ?>
